import {get_chat_message_content_list} from "@/api/message"
import { formatDate } from "@/utils/d_times"
import {ElNotification} from "element-plus";
export default {
  namespaced: true,
  state: {
    privateMessages: [],
    scrollBottom: 0,
  },
  mutations: {
    SET_PRIVATE_MESSAGE: (state, messages) => {
      state.privateMessages = messages
    },
    UPDATE_PRIVATE_MESSAGE_STATUS: (state, ids) => {
      state.privateMessages.forEach(item => {
        if (ids.includes(item.id)) {
          item.status = 1
        }
      })
    },
    PUSH_PRIVATE_MESSAGE: (state, message) => {
      state.privateMessages.push({
        ...message,
        createTimeAt: formatDate(message.createTime)
      })
    },

    UNSHIFT_PRIVATE_MESSAGE: (state, message) => {
      state.privateMessages.unshift(message)
    },
    UPDATE_PRIVATE_MESSAGE: (state, message) => {
      for (let i = 0; i < state.privateMessages.length; i++) {
        const item = state.privateMessages[i]
        if(item.ack === message.ack) {
          item.id = message.id
          item.loading = false
          item.createTime = message.createTime
          item.createTimeAt = formatDate(message.createTime)
          break
        }
      }
    },
    UPDATE_SCROLL_BOTTOM: (state) => {
      state.scrollBottom = state.scrollBottom + 1
    }
  },
  actions: {
    /**
     * 设置消息
     * @param commit
     * @param messages
     */
    setPrivateMessages({ commit }, messages = []) {
      commit('SET_PRIVATE_MESSAGE', messages)
    },
    /**
     * 消息滚动底部标识 变化 则滚动到底部
     * @param commit
     * @param message
     */
    updatePrivateMessage({commit}, message) {
      commit('UPDATE_PRIVATE_MESSAGE', message)
    },
    /**
     * 添加消息
     * @param message
     */
    pushPrivateMessage({commit}, message) {
      commit('PUSH_PRIVATE_MESSAGE', message)
    },
    /**
     * 更新会话消息滚动
     * @param commit
     */
    updateScrollBottom({ commit }) {
      commit('UPDATE_SCROLL_BOTTOM')
    },
    /**
     * 设置消息已读
     * @param commit
     * @param ids 消息Id数组
     */
    async updatePrivateMessageStatus({commit}, ids) {
      // const res = await messageStatusApi(ids)
      // commit('UPDATE_PRIVATE_MESSAGE_STATUS', ids)
      return ""
    },
    /**
     * 获取消息
     * @param commit
     * @param dispatch
     * @param params 获取消息参数
     * @param clear 是否清除旧信息
     */
    async getPrivateMessages({commit, dispatch}, {params, clear}) {
      let res = await get_chat_message_content_list(params)

      if(res) {
        if(clear) {
          dispatch("setPrivateMessages")
        }
        res.data.list.forEach(item => {
          item.createTimeAt = formatDate(item.createTime)
          commit('UNSHIFT_PRIVATE_MESSAGE', item)
        })
        return res
      }
    },
    /**
     * 提示信息
     * @param message
     */
    notificationMessage({commit}, message){
      ElNotification({
        title: '您有1条新消息!',
        message: message.content,
        type: 'success'
      })
    },
    /**
     * 通知新信息
     */
    newNotice(){
      window.postMessage({type: "FROM_INWUOO", text: "您有一条新消息"}, "*")
    }
  }
}